// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using NUnit.Framework;
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using WinPcap;

namespace WinPcap.Tests.Utils
{
	/// <summary>
	/// Test utilities
	/// </summary>
	public class TestUtility
	{
		/// <summary>
		/// Private constructor since this is a utility class and all
		/// methods are static.
		/// </summary>
		private TestUtility( )
		{
		}

		/// <summary>
		/// Compares two network interface objects to see if they 
		/// are the same.
		/// </summary>
		/// <param name="expectedNetworkInterface">
		/// The network interface holding the expected values.
		/// </param>
		/// <param name="actualNetworkInterface">The actual network interface to compare.</param>
		public static void CompareNetworkInterfaces( NetworkInterface expectedNetworkInterface,
			NetworkInterface actualNetworkInterface )
		{
			// Compare names.
			Assert.AreEqual( expectedNetworkInterface.Name, 
				actualNetworkInterface.Name,
				"Network interface names do not match." );

			// Compare descriptions.
			Assert.AreEqual( expectedNetworkInterface.Description, 
				actualNetworkInterface.Description,
				"Network interface descriptions do not match." );

			// Compare loopback flag.
			Assert.AreEqual( expectedNetworkInterface.IsLoopback, 
				actualNetworkInterface.IsLoopback,
				"Network interface loopback flags do not match." );

			// Compare data link layer type.
			Assert.AreEqual( expectedNetworkInterface.DataLinkLayerType, 
				actualNetworkInterface.DataLinkLayerType,
				"Network interface data link layer types do not match." );
		}


		/// <summary>
		/// Compares two network interface objects to see if they 
		/// are the same.
		/// </summary>
		/// <param name="expectedNetworkInterface">
		/// The network interface holding the expected values.
		/// </param>
		/// <param name="actualNetworkInterface">The actual network interface to compare.</param>
		public static void CompareNetworkInterfaceInfo( NetworkInterfaceInfo expectedNetworkInterface,
			NetworkInterface actualNetworkInterface )
		{
			// Compare descriptions.
			Assert.AreEqual( expectedNetworkInterface.Description, 
				actualNetworkInterface.Description,
				"Network interface descriptions do not match." );

			// Compare names.
			Assert.AreEqual( expectedNetworkInterface.Name, 
				actualNetworkInterface.Name,
				"Network interface names do not match." );

			// Compare loopback flags.
			Assert.AreEqual( expectedNetworkInterface.IsLoopback, 
				actualNetworkInterface.IsLoopback,
				"Network interface loopback flags do not match." );

			// Compare data link layer types.
			Assert.AreEqual( expectedNetworkInterface.DataLinkLayerType, 
				actualNetworkInterface.DataLinkLayerType.ToString( ),
				"Network interface data link layer types do not match." );
		}

		/// <summary>
		/// Compares two network address objects.
		/// </summary>
		/// <param name="expectedAddress">The expected network address.</param>
		/// <param name="actualAddress">The actual network address.</param>
		public static void CompareNetworkInterfaceAddress( NetworkInterfaceAddress expectedAddress,
			NetworkInterfaceAddress actualAddress )
		{
			Assert.Fail( "Not implemented comparison yet." );
		}

		/// <summary>
		/// Compares two network address objects.
		/// </summary>
		/// <param name="expectedAddress">The expected network address.</param>
		/// <param name="actualAddress">The actual network address.</param>
		public static void CompareNetworkInterfaceAddressInfo( NetworkInterfaceAddressInfo expectedAddress,
			NetworkInterfaceAddress actualAddress )
		{
			Assert.AreEqual( expectedAddress.Address,
				actualAddress.Address.ToString( ),
				"IP addresses do not match." );

			// Compare netmasks IP address, but not the port.
			string ActualNetmask = actualAddress.NetMask.ToString( );
			ActualNetmask = ActualNetmask.Substring( 0, ActualNetmask.IndexOf( ':' ) );
			
			Assert.AreEqual( expectedAddress.NetMask,
			                ActualNetmask,
				"Net masks do not match." );

			Assert.AreEqual( expectedAddress.BroadcastAddress,
				actualAddress.BroadcastAddress.Address.ToString( ),
				"Broadcast addresses do not match." );
		}

		/// <summary>
		/// Serializes a network interface to a file.
		/// </summary>
		/// <param name="networkInterface">The network interface object to
		/// serialize.</param>
		/// <param name="filename">The filename where the serialized
		/// object will be saved.</param>
		public static void SerializeNetworkInterfaceInfo( NetworkInterfaceInfo networkInterface,
			string filename )
		{
			XmlSerializer Serializer = new XmlSerializer( typeof( NetworkInterfaceInfo ) );

			XmlTextWriter Writer = new XmlTextWriter( filename, Encoding.UTF8 );

			Serializer.Serialize( Writer, networkInterface );

			Writer.Close( );
		}

		/// <summary>
		/// <para>Gets a byte array from an ascii string of hex numbers.  
		/// The string has the format:</para>
		/// <para>
		/// <code>
		/// 0000 AA BB CC DD 01 02 03 04  BB CC BB CC 01 02 02 01 
		/// 0010 DD EE
		/// </code>
		/// </para>
		/// </summary>
		/// <param name="data"></param>
		/// <returns></returns>
		public static byte[ ] GetBytesFromString( string data )
		{
			ByteArray Bytes = new ByteArray( );

			// Split the items.

			string[ ] DataItems = data.Split( ' ', '\n' );

			// Extract all bytes from the split items.
				
			for( int i = 0; i < DataItems.Length; ++i )
			{
				string Item = DataItems[ i ].Trim( );

				if( Item.Length == 2 )
				{
					byte ByteToAdd = Convert.ToByte( Item, 16 );
					Bytes.Add( ByteToAdd );
				}
				else
				{
					// Ignore item, it is probably the byte count at the 
					// start of the line.
				}
			}

			return Bytes.GetBytes( );
		}

		/// <summary>
		/// Compares two byte arrays starting from the specified indexes.
		/// </summary>
		/// <param name="expectedBytes">The expected bytes.</param>
		/// <param name="expectedBytesStartIndex">The index to start
		/// the comparison from.</param>
		/// <param name="actualBytes">The actual bytes.</param>
		/// <param name="actualBytesStartIndex">The index to start the comparison
		/// from.</param>
		/// <param name="amount">The number of bytes to compare.</param>
		public static void CompareBytes( byte[ ] expectedBytes, 
			int expectedBytesStartIndex,
			byte[ ] actualBytes,
			int actualBytesStartIndex,
			int amount )
		{
			int ExpectedBytesIndex = expectedBytesStartIndex;
			int ActualBytesIndex = actualBytesStartIndex;

			for( int i = 0; i < amount; ++i )
			{
				Assert.AreEqual( expectedBytes[ ExpectedBytesIndex ],
					actualBytes[ i + actualBytesStartIndex ],
					String.Concat( "Byte mismatch at expected byte index ", 
						ExpectedBytesIndex.ToString( ),
						" actual byte index ",
						ActualBytesIndex.ToString( ) ) );

				++ExpectedBytesIndex;
				++ActualBytesIndex;
			}
		}
	}
}
